$(function() {
	//设置活动按钮
	changeHeaderMenu(1);
	changeMenu(1, 3);
	//初始化列表数据
	initTable();
	savehandle();
	my_bind_check_num('#agentBase');
	my_bind_check_num('#agentBaseDef');
	loadAgentBaseDef();
});

function savehandle() {
	$('#alertdiv').hide();
	$('#alertdiv').savehandle({
		fieldMap : {},
		successMsg : "成功！",
		failMsg : "失败，请重试！",
		success : refresh
	});
}

/**
 * 初始化表格数据
 */
function initTable() {
	$('#grid').grid({
		title : '',
		pager : true,
		tbar : '#navbar',
		store : {
			url : '/fw/agent/agentbaseconfig_getlist.action',
			idProperty : 'agentName',
			uploadPagesize : 'rows'
		},
		pageCfg : {
			btnCount : 5
		},
		columns : [
		        {cn:'代理商名称', en:'agentName', width : 100},
		        {cn:'目前基数（x点/年）', en:'agentBase', width : 200},
		        {cn:'备注', en:'note', width : 100},
		        {cn:'编辑', en:'handle', width: 150, tpl: function(v) {
		        	var html = '';
		        	html += "<button onclick='edit(" + JSON.stringify(v) + ")' class='btn btn-success btn-xs'>修改</button>";
		        	return html;
		        }}
				]
	});
}

function add() {
	var agentName = $('#agentName').val();
	if(!agentName) {
		alert('请输入代理商名称');
		return;
	}
	var agentBase = $('#agentBase').val();
	if(!agentBase) {
		alert('请输入基数');
		return;
	}
	if(agentBase < 1) {
		alert('基数必须大于0');
		return;
	}
	var note = $('#note').val();
	var json = {
		agentName: agentName,
		agentBase: agentBase,
		note: note
	}
	$.getJSON('/fw/agent/agentbaseconfig_add.action', json, function(data){
		if(data.success && data.result) {
			alert('添加成功');
			refresh();
		} else {
			if(data.result.errors == '代理商基数配置已存在') {
				if(confirm('是否更新代理商“' + agentName + '”的基数点为' + agentBase)) {
					$.getJSON('/fw/agent/agentbaseconfig_edit.action', json, function(data){
						if(data.success && data.result) {
							alert('修改成功');
							refresh();
						} else {
							alert(data.result.errors?data.result.errors:'修改失败');
						}
					});
				}
			} else {
				alert(data.result.errors?data.result.errors:'添加失败');
			}
		}
	});
}

function edit(v) {
	$('#edit_agentName').html(v.agentName);
	$('#edit_agentBase').val(v.agentBase);
	$('#edit_note').val(v.note);
	$('#editModal').modal('show');
}

function edit_ok() {
	var agentName = $('#edit_agentName').html();
	if(!agentName) {
		alert('请输入代理商名称');
		return;
	}
	var agentBase = $('#edit_agentBase').val();
	if(!agentBase) {
		alert('请输入基数');
		return;
	}
	if(agentBase < 1) {
		alert('基数必须大于0');
		return;
	}
	var note = $('#edit_note').val();
	var data = {
		agentName: agentName,
		agentBase: agentBase,
		note: note
	}
	$.getJSON('/fw/agent/agentbaseconfig_edit.action', data, function(data){
		if(data.success && data.result) {
			alert('修改成功');
			$('#editModal').modal('hide');
			refresh();
		} else {
			alert(data.result.errors?data.result.errors:'修改失败');
		}
	});
}

/**
 * 刷新
 * 
 * @return {TypeName}
 */
function refresh() {
	$('#grid').grid('load', {data : {}});
}

var agentBaseDef;
function loadAgentBaseDef() {
	$.getJSON('/fw/agent/agentbasedef_get.action', function(data) {
		if(data.success && data.result) {
			$('#agentBaseDef').val(data.result);
			agentBaseDef = data.result;
		} else {
			$('#agentBaseDef').val("600");
			agentBaseDef = 600;
		}
	});
}

function edit_def() {
	var val = $('#agentBaseDef').val();
	if(!val) {
		alert('请输入默认基数');
		return;
	}
	if(val < 1) {
		alert('默认基数必须大于0');
		return;
	}
	var data = {
		agentBase: val
	};
	if(confirm('确认修改默认基数点为：' + val + '？')) {
		$.getJSON('/fw/agent/agentbasedef_edit.action', data, function(data) {
			if(data.success && data.result)
				alert('修改成功');
			else
				alert(data.result.errors?data.result.errors:'修改失败');
		});
	}
}